<template>
  <div class="main">
    <!-- 搜索表单 -->
    <div class="search">
      <el-form :inline="true">
        <el-form-item label="车牌号码">
          <el-input v-model="query.carNumber" />
        </el-form-item>
        <el-form-item label="车主姓名">
          <el-input v-model="query.personName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.cardStatus">
            <el-option v-for="item in cardStatusList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item></el-form>
      <el-button type="primary" @click="$router.push('/parking/card/addCard')">添加月卡</el-button>
      <el-button type="primary" @click="delCardList()">批量删除</el-button>
    </div>
    <!-- 月卡信息表 -->
    <el-table :data="cardList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column type="index" label="序号" />
      <el-table-column prop="personName" label="车主姓名" />
      <el-table-column prop="phoneNumber" label="联系方式" />
      <el-table-column prop="carNumber" label="车牌号码" />
      <el-table-column prop="carBrand" label="车辆品牌" />
      <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
      <el-table-column prop="cardStatus" :formatter="format" label="状态" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="text" @click="renewCard(scope.row.id)">续费</el-button>
          <el-button type="text" @click="cardDetail(scope.row.id)">查看</el-button>
          <el-button type="text" @click="editCard(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="deleteCard(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: right;"
      :current-page="query.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI } from '@/apis/carCard.js'
export default {
  data() {
    return {
      cardList: [],
      query: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        carStatus: ''
      },
      total: 0,
      cardStatusList: [
        { id: null, name: '全部' },
        { id: 0, name: '可用' },
        { id: 1, name: '已过期' }
      ],
      selectedCardList: []
    }
  },
  created() {
    this.getCardList()
  },

  methods: {
    // 查看月卡详情
    cardDetail(id) {
      this.$router.push({
        name: 'cardDetail',
        params: {
          id
        }
      })
    },
    // 续费月卡
    renewCard(id) {
      this.$router.push({
        name: 'renewCard',
        params: {
          id
        }
      })
    },
    // 批量删除月卡
    async delCardList() {
      if (this.selectedCardList.length <= 0) {
        this.$message.warning('请选择要删除的月卡')
      } else {
        this.$confirm('此操作将永久删除选择的月卡, 是否继续', '提示', {
          type: 'warning',
          confirmButtonText: '确认',
          cancelButtonText: '取消'
        }).then(async() => {
          console.log(this.selectedCardList)
          const ids = this.selectedCardList.map(item => {
            return item.id
          })
          await delCardAPI(ids)
          this.getCardList()
          this.$message.success('删除成功')
        }).catch()
      }
    },
    // 选中月卡改变项
    handleSelectionChange(rowList) {
      this.selectedCardList = rowList
    },
    // 删除月卡
    async deleteCard(id) {
      this.$confirm('此操作将永久删除选择的月卡, 是否继续', '提示', {
        type: 'warning',
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(async() => {
        await delCardAPI(id)
        this.getCardList()
        this.$message.success('删除成功')
      }).catch()
    },
    // 编辑月卡
    editCard(id) {
      this.$router.push({
        path: '/parking/card/addCard',
        query: {
          id
        }
      })
    },
    // 获取月卡列表
    async getCardList() {
      const res = await getCardListAPI(this.query)
      this.cardList = res.data.rows
      this.total = res.data.total
    },
    // 格式化状态
    format(row) {
      const MAP = {
        0: '可用',
        1: '已过期'
      }
      return MAP[row.cardStatus]
    },
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize
      this.getCardList()
    },
    handleCurrentChange(page) {
      // 把点击的页数赋值给请求参数页数
      this.query.page = page
      // 使用最新的请求参数获取列表数据
      this.getCardList()
    },
    search() {
      this.query.page = 1
      this.getCardList()
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  .el-form {
    .el-form-item {
      margin-right: 10px;
    }
  }
}

.el-pagination {
  text-align: right;
}
</style>
